const {
  baseURL
} = require("../fetch/http")
const app = getApp()
const api = require('../fetch/api')
const {
  navBarHeight,
  statusBarHeight,
  menuButton
} = app.globalData
Component({
  pageLifetimes: {
    show() {
      if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          selected: 1
        })
      }
      this.setData({
        page:1,
        arr:[]
      })
    this.goods()
    }
  },
  data: {
    baseURL: baseURL,
    statusBarHeight: statusBarHeight,
    h: navBarHeight,
    w: menuButton.width,
    right: menuButton.right,
    bottomHeight: navBarHeight - statusBarHeight,
    tabs: [],
    tabIndex: 0,
    toView: 's0',
    arr: [],
    loading: false,
    finished: false,
    page: 1,
    goods_classify_id:""
  },
  async attached() {
    wx.loadFontFace({
      family: "Font_Bold",
      source: 'url("https://yd.soft.haoyangsoft.com/static/wx/ttf/SM2.OTF")'
    });
   
   
    this.goods()
  },
  methods: {
    async goods() {
      if (!this.data.loading) {
        this.setData({
          loading: true
        })
        let res = await api.goods({
          page: this.data.page,
          goods_type: 1,
          goods_classify_id: this.data.goods_classify_id
        })
        let finished = false
        res.data.length == 0 && (finished = true)
      console.log(res,11)
        this.setData({
          // arr: [...this.data.arr,...res.data],
          arr: this.data.arr.concat(res.data),
          finished,
          loading: false
        })
      }
    },
    search() {
      wx.navigateTo({
        url: '/pages/home/search/index',
      })
    },
    goodsDetail(e) {
      const {id} = app.dataSet(e)
      wx.navigateTo({
        url: '/pages/mall/goodsDetail/index?id=' + id,
      })
    },
    switchTab(e) {
      const { index } = app.dataSet(e)
      if (this.data.tabIndex != index) {
        this.setData({
          tabIndex: index,
          goods_classify_id: this.data.tabs[index].id,
          toView: 's' + index
        })
        this.clear()
        this.goods()
      }
    },  
    clear() {
      this.setData({
        page: 1,
        loading: false,
        finished: false,
        arr: []
      })
    },
    go_class(){
     wx.navigateTo({
       url: '/pages/mall/productClass/index',
     })
    },
    onReachBottom() {
      let {
        page,
        finished
      } = this.data
      if (!finished) {
        this.setData({
          page: page + 1
        })
        this.goods()
      }
    },
    onShareAppMessage: function (res) {
      if (!wx.getStorageSync('token')) {
        app.toast('请登录')
        return false
      }
      return {
        imageUrl: this.data.baseURL + '/img/share_img_2.png',
        title: '天喜鑫宝',
        path: '/index/index4?id=' + wx.getStorageSync('id')
      }
    }
  }
})